package MyView;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

/**
 * Created by dell on 2017/3/30.
 */

public class MyDecoration extends RecyclerView.ItemDecoration {
    private int space;
    private int span;

    public MyDecoration(int space, int span) {
        this.space = space;
        this.span = span;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.bottom = space;
        if ( parent.getChildAdapterPosition(view)==0||parent.getChildLayoutPosition(view) == parent.getAdapter().getItemCount()) {
            outRect.left = space;
            outRect.right = space;
            outRect.top=space;
        } else if (parent.getLayoutManager() instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
            int spanIndex = lp.getSpanIndex();
            //判断当前的位置，如果是最后设置右边距
            if (spanIndex == span-1) {
                outRect.left = space;
                outRect.right = space;
            } else {//其余的位置右边距为0
                outRect.left = space;
                outRect.right = 0;
            }
        } else if (parent.getLayoutManager() instanceof GridLayoutManager){
            if (parent.getChildLayoutPosition(view) % span == 0) {
                outRect.left = space;
                outRect.right = space;
            } else {
                outRect.left = space;
                outRect.right = 0;
            }
        } else if (parent.getLayoutManager() instanceof LinearLayoutManager){
            outRect.left=space;
            outRect.right=space;
        }
    }

//    private Context context;
//    private Drawable mdivider;
//    public static int[] ATTRS = new int[]{
//            android.R.attr.listDivider
//    };
//
//    public MyDecoration(Context context) {
//        this.context = context;
//        final TypedArray ta = context.obtainStyledAttributes(ATTRS);
//        this.mdivider = ta.getDrawable(0);
//        ta.recycle();
//    }
//
//    @Override
//    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//        super.onDraw(c, parent, state);
//        drawHorizonalLine(c, parent);
//
//    }
//
//    public void drawHorizonalLine(Canvas c, RecyclerView parent) {
//        int left = parent.getPaddingLeft();
//        int right = parent.getWidth() - parent.getPaddingRight();
//        final int childCount = parent.getChildCount();
//        for (int i = 0; i < childCount - 1; i++) {
//            final View child = parent.getChildAt(i);
//            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
//            final int top = child.getBottom() + params.bottomMargin;
//            final int bottom = top + mdivider.getIntrinsicHeight();
//            mdivider.setBounds(left, top, right, bottom);
//            mdivider.draw(c);
//        }
//    }
//
//
//    //偏移量
//    @Override
//    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//        super.getItemOffsets(outRect, view, parent, state);
//        outRect.set(0, 0, mdivider.getIntrinsicWidth(), 0);
//    }
}
